<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="form.data.id ? '修改会员类别' : '新增会员类别'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="150">
          <FormItem prop="memberTypeName" label="会员类别名称">
            <Input v-model="form.data.memberTypeName" placeholder="请输入"></Input>
          </FormItem>
          <FormItem prop="memberTypeMemo" label="会员类别说明">
            <Input type="textarea" v-model="form.data.memberTypeMemo" placeholder="请输入" :rows="4"></Input>
          </FormItem>
          <FormItem prop="scoreTimes" label="积分倍数">
            <InputNumber :max="9999" v-model.number="form.data.scoreTimes" placeholder="请输入"></InputNumber>
          </FormItem>
          <FormItem prop="isEnable" label="是否外网显示">
            <Select v-model="form.data.isEnable" placeholder="请选择是否外网显示">
              <Option :value="0">显示</Option>
              <Option :value="-1">不显示</Option>
            </Select>
           </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import { mapGetters } from 'vuex'
  import vipApi from '@/api/vip'

  export default {
    name: 'update-modal',
    props: [
      'form'
    ],
    data () {
      return {
        loading: true,
        ruleValidate: {
          memberTypeName: [
            { required: true, message: '请输入会员类别名称', trigger: 'change' },
            { max: 50, message: '会员类别名称字符长度应小于50', trigger: 'change' }
          ],
          memberTypeMemo: [
            { required: true, message: '请输入会员类别说明', trigger: 'change' },
            { max: 100, message: '会员类别说明字符长度应小于100', trigger: 'change' }
          ],
          scoreTimes: [
            { required: true, type: 'number', message: '请输入正确的积分倍数', trigger: 'change' }
          ],
          isEnable: [
            { required: true, type: 'number', message: '请选择是否外网显示', trigger: 'change' }
          ]
        }
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo'
      })
    },
    watch: {
      'form.visible': 'onClose'
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$nextTick(() => {
            this.$refs.form.resetFields()
          })
        }
      },
      async submitForm () {
        this.form.data.pkRcId = this.userInfo.pkRclubInfo
        if (this.form.data.id) {
          await vipApi.updateType(this.form.data)
          this.$Message.info('修改成功')
        } else {
          await vipApi.createType(this.form.data)
          this.$Message.info('创建成功')
        }
        this.form.visible = false
        this.$emit('submit')
      }
    }
  }
</script>
